/////////////////////////////////////////////////////////////////////////////
*Figure 1 Mincer Gender Wage and Employment Gaps, by age
/////////////////////////////////////////////////////////////////////////////

est clear

local regvar regular1

foreach var in working  logwage  `regvar' {
	quietly{
eststo clear

//Load data
use Data\klips_workingdata_hh, clear

//Keep if years 2010-19, age 25-54
keep if years>=2010 & years<=2019
keep if age>=25 & age<=54

//Define cross-sectional weight for multi-year analysis for only those 25-54: reweight to sum to 100 in each year so that no year weights more than another year in a multi-year regression
foreach weightvar in _weight09_ _weight18_{
bysort years: egen temp_tot`weightvar' = total(`weightvar')
gen temp_`weightvar'_new = 100*`weightvar'/temp_tot`weightvar'
}
drop temp_*
gen _weight2554_ = _weight09__new if years>=2009 & years<2018
replace _weight2554_ = _weight18__new if years>=2018

//Specify regression options
local weight [pw=_weight2554_]
local stderrors robust cluster(pid)


//Generate age-specific female dummies
local regvars
		forvalues i = 25(5)50{
		local j = `i' + 4
			capture drop female_`i'_`j'
			gen female_`i'_`j' = (female==1 & age>=`i' & age<=`j')
			lab var female_`i'_`j' "`i'-`j'"
			local regvars `regvars' female_`i'_`j'
		}	


local col 
local nocol
local col2
local i 
local j 
local plotvars
local tabvars

//Controls
	global nocontrols i.years i.age  i.residence
	global controls i.years i.age i.edu  i.residence
	global midcontrols  i.years i.age i.edu i.residence i.ind i.occ 
	global fullcontrols  i.years i.age i.edu i.residence i.ind i.occ worktime i.status i.union i.workplace
	
//Options for reressions and figures for different dependent variables
if "`var'"=="working"{
	global depvar `var'
	global label employment
	global title A: $label
	global cond if !missing(working) & !missing(age) & !missing(edu) & !missing(residence)
	global axes xsc(r(-0.55 0.15 )) xlabel(-0.5 (0.1) 0.1)
}
if "`var'"=="logwage"{
	global depvar `var'
	global cond if !missing(working) & !missing(age) & !missing(edu) & !missing(residence) & !missing(ind) & !missing(occ) & !missing(worktime) & !missing(status) & !missing(union) & !missing(workplace)
	global label log wage
	global title B: $label
	global axes xsc(r(-0.55 0.15 )) xlabel(-0.5 (0.1) 0.1)
	global legend legend(title("{bf: Controls}") pos(6) cols(4) order(2 "Year, age, region" 4 "+ education" 6 "+occ, ind" 8 "+ hours, work status, union, workplace type"))
}
if "`var'"=="`regvar'"{
	global depvar `regvar'
	global cond if !missing(working) & !missing(age) & !missing(edu) & !missing(residence) & !missing(ind) & !missing(occ)
	global label regular job
	global title C: $label
	global axes  xsc(r(-0.55 0.15 )) xlabel(-0.5 (0.1) 0.1)
}




***********************
//Regressions - all ages

	// Display sample size
	noisily{
	di "Sample size: `var'"
	di "Observations"
	count $cond
	}
		preserve
			 keep $cond
			 keep pid 
			 duplicates drop
			 	noisily{
			 di "Unique individuals"
			 count
				}
		restore
	
	//Generate separate female dummy for age group (for ease of axis labeling on coefficient plot)
	lab var female "{bf: All (25-54)}"
	
	//No controls
	qui : reg $depvar female $nocontrols $cond  `weight', `stderrors'
	est sto e_0_all
	local nocol `nocol' e_0_all 
	
	//Basic controls (age, education, residence)
	qui : reg $depvar female  $controls $cond  `weight', `stderrors'
	est sto e_1_all
	local col `col' e_1_all
	
//Mid-level controls (adding occ, ind)
if "`var'"!="working" {
	qui : reg $depvar female  $midcontrols $cond `weight', `stderrors'
	est sto e_2_all
	local col2 `col2' e_2_all
}

	//Full controls (occ, ind, workplace, job)
if "`var'"=="logwage"{
	qui : reg $depvar female  $fullcontrols $cond  `weight', `stderrors'
	est sto e_3_all
	local col3 `col3' e_3_all
}
	
	
***********************
//Regressions - by age group 

//By age
	//No controls
	qui : reg $depvar `regvars'  $nocontrols $cond  `weight', `stderrors'
	est sto e_0_byage
	local nocol `nocol' e_0_byage
	
	//Basic controls (age, education, residence)
	qui : reg $depvar `regvars'  $controls $cond `weight', `stderrors'
	est sto e_1_byage
	local col `col' e_1_byage
	
	//Mid-level controls (adding occ, ind)
if "`var'"!="working" {
	qui : reg $depvar `regvars'  $midcontrols $cond `weight', `stderrors'
	est sto e_2_byage
	local col2 `col2' e_2_byage
}
	//Full controls (occ, ind, workplace, job)
if "`var'"=="logwage"{
	qui : reg $depvar `regvars'  $fullcontrols $cond  `weight', `stderrors'
	est sto e_3_byage
	local col3 `col3' e_3_byage
}


//Options for coefficient plot
if "`var'"=="working"{
local plotvars  (`nocol',   mcolor(dkgreen) ciopts(lcolor(dkgreen%30) lwidth(thick)) offset(0.1)) (`col', mcolor(dkorange) ciopts(lcolor(dkorange%30) lwidth(thick)) offset(-0.1))
local tabvars `nocol' `col'
}
if "`var'"=="logwage"{
local plotvars (`nocol',  mcolor(dkgreen) ciopts(lcolor(dkgreen%30) lwidth(thick))offset(0.25)) (`col', mcolor(dkorange) ciopts(lcolor(dkorange%30) lwidth(thick)) offset(0.1))  (`col2', mcolor(purple) ciopts(lcolor(purple%30) lwidth(thick)) offset(-0.1))  (`col3', mcolor(blue) ciopts(lcolor(blue%30) lwidth(thick)) offset(-0.25)) 
local tabvars `nocol' `col' `col2' `col3'
}
if "`var'"=="`regvar'"{
local plotvars (`nocol',  mcolor(dkgreen) ciopts(lcolor(dkgreen%30) lwidth(thick))offset(0.15)) (`col', mcolor(dkorange) ciopts(lcolor(dkorange%30) lwidth(thick)) offset(0))  (`col2', mcolor(purple) ciopts(lcolor(purple%30) lwidth(thick)) offset(-0.15)) 
local tabvars `nocol' `col' `col2'	
}

//Estimates
noisily{
di "`var'" 
esttab `tabvars', keep(female*)
}

//Coefficient plot
coefplot `plotvars' ///
 , keep(female*) $legend  $axes ylabel(, angle(45))  xline(0, lwidth(thin) lcolor(red)) title("$title") xtitle("Gender $label gap" "2010-19, (men=0)") headings(female_25_29 = "{bf: By age}", angle(45)) yline(2, lwidth(thin) lpattern(solid)) yline(2.1, lwidth(thin) lpattern(solid))
 graph save Figures\temp_`var'_byage.gph, replace
	}
 }

 

 
 //Figure 1
 grc1leg Figures\temp_working_byage.gph Figures\temp_logwage_byage.gph Figures\temp_`regvar'_byage.gph, cols(3) legend(Figures\temp_logwage_byage.gph)
 graph export Figures\Figure1.pdf, replace

 
 //Erase intermediate figures
 foreach var in working logwage `regvar'{
 erase Figures\temp_`var'_byage.gph 
 }
 